Index: includes/common.inc
===================================================================
--- includes/common.inc	(revision 3046)
+++ includes/common.inc	(working copy)
@@ -2650,6 +2650,7 @@
   module_load_all();
   // Let all modules take action before menu system handles the request
   // We do not want this while running update.php.
+  memory_put('bootstrap');
   if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') {
     module_invoke_all('init');
   }
Index: includes/module.inc
===================================================================
--- includes/module.inc	(revision 3046)
+++ includes/module.inc	(working copy)
@@ -11,7 +11,9 @@
  */
 function module_load_all() {
   foreach (module_list(TRUE, FALSE) as $module) {
+    memory_put('code', $module);
     drupal_load('module', $module);
+    memory_put('code', $module);
   }
 }
 
Index: includes/theme.inc
===================================================================
--- includes/theme.inc	(revision 3046)
+++ includes/theme.inc	(working copy)
@@ -167,12 +167,16 @@
     foreach ($base_theme as $base) {
       // Include the theme file or the engine.
       if (!empty($base->owner)) {
+        memory_put('code', $base->owner);
         include_once './'. $base->owner;
+        memory_put('code', $base->owner);
       }
     }
     // and our theme gets one too.
     if (!empty($theme->owner)) {
+      memory_put('code', $theme->owner);
       include_once './'. $theme->owner;
+      memory_put('code', $theme->owner);
     }
   }
 
@@ -288,13 +292,17 @@
         // First, check to see if the fully qualified file exists.
         $filename = './'. $path .'/'. $info['file'];
         if (file_exists($filename)) {
+          memory_put('code', $filename);
           require_once $filename;
+          memory_put('code', $filename);
           $result[$hook]['include files'][] = $filename;
         }
         else {
           $filename = './'. $info['file'];
           if (file_exists($filename)) {
+            memory_put('code', $filename);
             require_once $filename;
+            memory_put('code', $filename);
             $result[$hook]['include files'][] = $filename;
           }
         }
@@ -302,7 +310,9 @@
       elseif (isset($info['file']) && isset($info['path'])) {
         $filename = './'. $info['path'] .'/'. $info['file'];
         if (file_exists($filename)) {
+          memory_put('code', $filename);
           require_once $filename;
+          memory_put('code', $filename);
           $result[$hook]['include files'][] = $filename;
         }
       }
@@ -629,7 +639,9 @@
   // Include a file if the theme function or preprocess function is held elsewhere.
   if (!empty($info['include files'])) {
     foreach ($info['include files'] as $include_file) {
+      memory_put('code', $include_file);
       include_once($include_file);
+      memory_put('code', $include_file);
     }
   }
 
@@ -646,7 +658,9 @@
       // run unnecessary file_exists calls.
       $included_files[$include_file] = TRUE;
       if (file_exists('./'. $include_file)) {
+        memory_put('code', $include_file);
         include_once('./'. $include_file);
+        memory_put('code', $include_file);
       }
     }
   }
@@ -1063,7 +1077,9 @@
 function theme_render_template($template_file, $variables) {
   extract($variables, EXTR_SKIP);  // Extract the variables to a local namespace
   ob_start();                      // Start output buffering
+  memory_put('code', $template_file);
   include "./$template_file";      // Include the template file
+  memory_put('code', $template_file);
   $contents = ob_get_contents();   // Get the contents of the buffer
   ob_end_clean();                  // End buffering and discard
   return $contents;                // Return the contents
@@ -1621,6 +1637,7 @@
  *   A string containing the themed blocks for this region.
  */
 function theme_blocks($region) {
+  memory_put('blocks', $region);
   $output = '';
 
   if ($list = block_list($region)) {
@@ -1632,7 +1649,8 @@
 
   // Add any content assigned to this region through drupal_set_content() calls.
   $output .= drupal_get_content($region);
-
+  memory_put('blocks', $region);
+  
   return $output;
 }
 
Index: index.php
===================================================================
--- index.php	(revision 3046)
+++ index.php	(working copy)
@@ -12,6 +12,10 @@
  * See COPYRIGHT.txt and LICENSE.txt.
  */
 
+include_once './includes/memory.inc';
+memory_put('total');
+memory_put('bootstrap');
+ 
 require_once './includes/bootstrap.inc';
 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 
@@ -37,3 +41,6 @@
 }
 
 drupal_page_footer();
+
+memory_put('total');
+memory_shutdown();
\ No newline at end of file
Index: themes/engines/phptemplate/phptemplate.engine
===================================================================
--- themes/engines/phptemplate/phptemplate.engine	(revision 3046)
+++ themes/engines/phptemplate/phptemplate.engine	(working copy)
@@ -12,7 +12,9 @@
 function phptemplate_init($template) {
   $file = dirname($template->filename) .'/template.php';
   if (file_exists($file)) {
+    memory_put('code', $file);
     include_once "./$file";
+    memory_put('code', $file);
   }
 }
 
Index: includes/memory.inc
===================================================================
RCS file: includes/memory.inc
diff -N includes/memory.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ includes/memory.inc	23 Oct 2008 02:21:38 -0000
@@ -0,0 +1,93 @@
+<?php
+
+$mem = array();
+
+function memory_put($realm = NULL, $item = NULL) {
+  global $mem;
+
+  if (!isset($realm)) {
+    // Nothing to do
+    return;
+  }
+
+  if (!isset($item)) {
+    if (isset($mem[$realm])) {
+      // Has a value, so calculate the memory usage
+      $mem[$realm] = memory_get_usage() - $mem[$realm];
+    }
+    else {
+      // Not set before, set it now
+      $mem[$realm] = memory_get_usage();
+    }
+  }
+  else {
+    if (isset($mem[$realm][$item])) {
+      $mem[$realm][$item] = memory_get_usage() - $mem[$realm][$item];
+    }
+    else {
+      $mem[$realm][$item] = memory_get_usage();
+    }
+  }
+}
+
+function memory_get($realm = NULL, $item = NULL) {
+  global $mem;
+  
+  if (!$realm) {
+    // Nothing to do
+    return 0;
+  }
+
+  if (!$item) {
+    return $mem[$realm];
+  }
+  else {
+    return $mem[$realm][$item];
+  }
+}
+
+function memory_get_file() {
+  static $memory_file;
+
+  if ($memory_file) {
+    return $memory_file;
+  }
+
+  $tmp_dir = variable_get('file_directory_temp', '/tmp');
+  $memory_file = fopen($tmp_dir . '/memory.csv', 'a+');
+
+  // Output the path of the page, since different pages can have different memory
+  memory_write(':==' . date('c') . '===  Page: ' . $_GET['q']);
+  return $memory_file;
+}
+
+function memory_write($string = '') {
+  // Add the following line to settings.php to enable memory profiling
+  //   $conf['memory_profiling'] = 1;
+  if (variable_get('memory_profiling', 0)) {
+    $file = memory_get_file();
+    fwrite($file, $string . "\n");
+  }
+}
+
+function memory_shutdown() {
+  $realm_data = array();
+
+  foreach(array('code', 'blocks') as $realm) {
+    $realm_data[$realm] = 0;
+    foreach(memory_get($realm) as $item => $bytes) {
+      $realm_data[$realm] += $bytes;
+      memory_write($realm . '-' . $item . ',' . $bytes);
+    }
+  }
+
+  $drupal = memory_get('total');
+  $data = $drupal - $realm_data['code'];
+
+  memory_write('Total code,'     . $realm_data['code']);
+  memory_write('Bootstrap code,' . memory_get('bootstrap'));
+  memory_write('Block data,'     . $realm_data['blocks']);
+  memory_write('Total data,'     . $data);
+  memory_write('Drupal,'         . $drupal);
+  memory_write('PHP,'            . memory_get_peak_usage());
+}